package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 检查提交报告（检查情况说明）
 * BT_ITEM_CHECK_REPORT_BASIC 	===> nd_submit_inspection_report
 */
@BusinessDataConverter(
        insertSql = "INSERT INTO ND_SUBMIT_INSPECTION_REPORT(ID, BPM_STATUS, CREATE_NAME, CREATE_BY, UPDATE_NAME, UPDATE_BY, SYS_ORG_CODE, SYS_COMPANY_CODE, CREATE_DATE, UPDATE_DATE, PRO_ID, COOPERATE_ATTITUDE, INSPECTION_TYPE, ALARM_LEVEL, CHECK_DATE, RECEPTION_NAME, CONTACT_NUMBER, CHECK_DESCRIPTION, REPORT_RATING, RISK_CLASSIFICATION) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE ND_SUBMIT_INSPECTION_REPORT SET BPM_STATUS = ?, CREATE_NAME = ?, CREATE_BY = ?, UPDATE_NAME = ?, UPDATE_BY = ?, SYS_ORG_CODE = ?, SYS_COMPANY_CODE = ?, CREATE_DATE = ?, UPDATE_DATE = ?, PRO_ID = ?, COOPERATE_ATTITUDE = ?, INSPECTION_TYPE = ?, ALARM_LEVEL = ?, CHECK_DATE = ?, RECEPTION_NAME = ?, CONTACT_NUMBER = ?, CHECK_DESCRIPTION = ?, REPORT_RATING = ?, RISK_CLASSIFICATION = ? WHERE ID = ?",
        findMigratedIdListSql = " select ID from ND_SUBMIT_INSPECTION_REPORT where PRO_ID = ?",
        seq = 210
)
public class NdSubmitInspectionReport extends AbstractBusiness {

    public NdSubmitInspectionReport(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws Exception {

        List<Map> btItemCheckReportBasicList = this.sourceData.getBtItemCheckReportBasic();

        for (Map<String,Object> btItemCheckReportBasic : btItemCheckReportBasicList) {
            // 判断是否需要覆盖迁移
            int needMigratedResult = this.needMigrated(buildId(btItemCheckReportBasic));
            if(needMigratedResult == 0) {
                continue;
            }

            // 单条插入或更新
            if(needMigratedResult == 1) {
            this.targetDbUtil.updateByParams(updateSql,this.buildValues(btItemCheckReportBasic),true);
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchUpdateValues.add(this.buildValues(btItemCheckReportBasic));
            } else {
            this.targetDbUtil.updateByParams(insertSql,this.buildValues(btItemCheckReportBasic));
                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchInsertValues.add(this.buildValues(btItemCheckReportBasic));
            }
        }


    }

    public List buildValues(Map btItemFareDetail) throws Exception{

        List values = new ArrayList();

        //ID
        values.add(buildId(btItemFareDetail));
        //BPM_STATUS	流程状态
        values.add(1);
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //CREATE_DATE	创建日期
        values.add(btItemFareDetail.get("CREATE_TIME"));
        //UPDATE_DATE	更新日期
        values.add(btItemFareDetail.get("MODIFY_TIME"));

        //PRO_ID	项目ID
        values.add(this.projectId);
        //COOPERATE_ATTITUDE	配合态度
        values.add(btItemFareDetail.get("SUPPORTATTITUDE"));
        //INSPECTION_TYPE	检查类型
        values.add(btItemFareDetail.get("CHECKTYPE"));
        //ALARM_LEVEL	报警级别
        values.add(btItemFareDetail.get("ALARMLEVEL"));
        //CHECK_DATE	检查日期
        values.add(btItemFareDetail.get("CHECKDATE"));
        //RECEPTION_NAME	接待人名
        values.add(btItemFareDetail.get("RECCEPTERNAME"));
        //CONTACT_NUMBER	联系电话
        values.add(btItemFareDetail.get("RECCEPTERTEL"));
        //CHECK_DESCRIPTION	检查情况说明
        values.add(Tool.replaceBrFlagStr(btItemFareDetail.get("NOTICEMEMO")));
        //REPORT_RATING	报告评分
        values.add(btItemFareDetail.get("REPORTMARK"));
        //RISK_CLASSIFICATION	风险分类
        values.add(btItemFareDetail.get("ALARMLEVEL"));


        return values;
    }

    public String buildId(Map btItemFareDetail){
        // 基础表数据业务主键
        String originalId = btItemFareDetail.get("CHECKNO").toString();
        return originalId;
    }
}
